const config = require('../config');
const mService = require('../service/mysqlService')
const utils = require('../utils/util')


test = async(ctx,next)=>{
   let ds =  await mService.getNextData(1111);
   console.log(JSON.stringify(ds))
}


getNextData = async(ctx,next)=>{
    let id = ctx.query.id;

    if(id < 0 || id == undefined || isNaN(id)){
        id = await mService.getLatestIndex(); //如果没有初始id
    }else{ id++;}

    let ds =  await mService.getNextData(id);

    ctx.state.data = {
        ds:ds,
    }
}

getDsById = async(ctx,next)=>{
    let id = ctx.query.id;
    let num = ctx.query.num;
    if(!num>0){num = 1;}

    if(id < 0 || id == undefined || isNaN(id)){
        id = await mService.getLatestIndex(); //如果没有初始id
    }

    let ds =  await mService.getDsById(id,num);

    ctx.state.data = {
        ds:ds,
    }
}

getSsById = async(ctx,next)=>{
    let id = ctx.query.id;
    let sId = ctx.query.sId;
    let num = ctx.query.num;
    if(!num>0){num = 1;}
    if(sId == undefined || isNaN(sId)) sId = 0;

    if(id < 0 || id == undefined || isNaN(id)){
        id = await mService.getLatestIndex(); //如果没有初始id
    }else{ id++;}

    let ds =  await mService.getSsById(id,sId,num);

    ctx.state.data = {
        ds:ds,
    }
}

getServerNextData = async(ctx,next)=>{
    let id = ctx.query.id;
    let sId = ctx.query.sId;

    if(sId == undefined || isNaN(sId)) sId = 0;

    if(id < 0 || id == undefined || isNaN(id)){
        id = await mService.getLatestIndex(); //如果没有初始id
    }else{ id++;}

    let ds =  await mService.getServerNextData(id,sId);

    ctx.state.data = {
        ds:ds,
    }
}

getIdRange = async(ctx,next) =>{
    let st = ctx.query.st;
    let et = ctx.query.et;

    let idRange = [-1,-1];
    if(st == undefined || isNaN(st) || et == undefined || isNaN(et) || st >= et) {}
    else{
        idRange = await mService.getIdRange(st,et);
    };

    ctx.state.data = {
        idRange:idRange
    }
}


getSnapshotInit = async(ctx,next) =>{

    let tsRange = await mService.getTsRange();

    let id = await mService.getLatestIndex()
    let ds = await mService.getNextData(id);

    ctx.state.data = {
        ds: ds,
        tsRange:tsRange
    }
}


module.exports = {
    test,getNextData,getServerNextData,getIdRange,getSnapshotInit,
    getSsById,getDsById
}